package com.eud.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.eud.entity.Order;
import com.eud.entity.User;
import com.eud.service.OrderService;
import com.eud.service.UserService;
import com.eud.service.impl.OrderServiceImpl;
import com.eud.service.impl.UserServiceImpl;

/**
 * Servlet implementation class SetServlet
 */
@WebServlet("/SeatServlet")
public class SeatServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SeatServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		
		response.setCharacterEncoding("UTF-8");
		
		UserService  u=new UserServiceImpl();
		OrderService or =new OrderServiceImpl();
		
		HttpSession session=request.getSession();
		
		String name=(String)session.getAttribute("user");
		User u1=u.query(name);
		String seat=request.getParameter("seat");
		String moviename=request.getParameter("moviename");
		String o=request.getParameter("time");
		String o1=request.getParameter("t");
		String[] array = null ;
		 if (o1 != null && !o1.isEmpty()) {
	            // 使用 split 方法将字符串分割成数组
	             array = o1.split(",");
	             System.err.println(array);
		 }
		String num=request.getParameter("num");
		String time=request.getParameter("time1");
		System.err.println(time);
		int num1=0;
		if(num!=null) {
			 num1=Integer.parseInt(request.getParameter("num"));
		}
		
		double priceAll=0;
		if(request.getParameter("priceAll")!=null) {
			 priceAll=Double.parseDouble(request.getParameter("priceAll"));
		}
		
		double pri=0;
		if(num1!=0 && priceAll!=0) {
			pri=priceAll/num1;
		}
		
		Order oord;
		 Boolean b;
		 String a=seat.replaceAll("\\s*", "");
		
		 
        if(a!=null) {
        	int i=0;
        	while(a.length() > 0) {
        		i++;
                String firstFourChars = a.substring(0, Math.min(a.length(), 6));
              if(i<=num1) {
            	  oord=new Order(u1.getuId(),array[i-1],moviename,time,"1号激光厅",firstFourChars,pri,o,"时光影院");
            	 
                  b=or.addOrder(oord);
                 
                  
                  
                  
              }
     
                if(a.length() > 6) {
                	
                	a = a.substring(6);
                } else {
                    break;
                }
            }
        }
        response.sendRedirect("buyticket.jsp");
      //request.getRequestDispatcher("buyticket.jsp").forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		
		UserService  u=new UserServiceImpl();
		OrderService or =new OrderServiceImpl();
		  List<Order> l=or.querySeat();
	      
			
			
			
			List<String> l1=new ArrayList();
			for (Order order : l) {
				String pattern="(\\d{2})排(\\d{2})座";
				Pattern r =Pattern.compile(pattern);
				Matcher m=r.matcher(order.getOrder_position());
			
				if(m.find()) {
					int row=Integer.parseInt(m.group(1));
					int se=Integer.parseInt(m.group(2));
					String result=row+"_"+se;
					l1.add(result);
				}
			}
			String q="\'\'";
			String s=",";
			String e="";
			List li=new ArrayList();
			for (int i = 0; i<l1.size();i++) {
				
					String b2=l1.get(i);
					e=""+b2+"";
			
					li.add(e);
			}
			
			String j=JSON.toJSONString(li);
		
			PrintWriter p1=response.getWriter();
			p1.print(j);
			p1.flush();
			p1.close();
				
	}

}
